package com.lin.filegraph.utils.moduledg;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 15:26
 */

public class PomNode {
    private String name;
    private List<PomNode> children = new ArrayList<>();
    private Integer childNum = 0;
    private String parent;
    private String path;
    public PomNode(File pom){
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        Document doc = null;
        try {
            db = dbf.newDocumentBuilder();
            doc = db.parse(pom);
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
        if(doc==null){
            return;
        }
        NodeList arts = doc.getElementsByTagName("artifactId");
        for (int i=0;i<arts.getLength();i++){
            if("parent".equals(arts.item(i).getParentNode().getNodeName())){
                parent = arts.item(i).getTextContent();
            }
            else if("project".equals(arts.item(i).getParentNode().getNodeName())){
                name = arts.item(i).getTextContent();
            }
        }
        path = pom.getParent();
    }
    public void addChild(PomNode child){
        children.add(child);
        childNum+=1;
    }
    public boolean hasNotChildren(){
        return childNum == 0;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getChildNum() {
        return childNum;
    }

    public void setChildNum(Integer childNum) {
        this.childNum = childNum;
    }

    public String getParent() {
        return parent;
    }

    public void setParent(String parent) {
        this.parent = parent;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}

